Page({
    data:{
      // 操作类型标记：true-立即购买，false-加入购物车
      isBuyNow: false,
      // 轮播图
      banner:[
        'https://img.alicdn.com/i2/2207564291145/O1CN015g04zh1KKSQ9gKgos_!!2207564291145.jpg',
        'https://ts1.tc.mm.bing.net/th/id/R-C.6f6b89eba45a1cd65168bdf3dddb316a?rik=9rYxhoEYxx%2fX4g&riu=http%3a%2f%2f5b0988e595225.cdn.sohucs.com%2fimages%2f20180817%2f83165053f1a54f2eb3e301a0cbc31707.jpg&ehk=eFhKNWfyCUb1KALKVSqYJRM9dEoujFY%2bioWHwdLGgWc%3d&risl=&pid=ImgRaw&r=0'
      ],
      
      title:'老上海鲜得来排骨年糕',
      desc:'美味经典',
      price:118,
      express:'免运费',
      sales:200,
      city:'广州',
      richText:'<img src="https://img.alicdn.com/i2/2207564291145/O1CN015g04zh1KKSQ9gKgos_!!2207564291145.jpg" style="max-width:100%;height:auto"/>',
  
      // 收藏
      isFav:false,
  
      // 规格弹窗
      showSku:false,
      skuCover:'https://img.alicdn.com/i2/2207564291145/O1CN015g04zh1KKSQ9gKgos_!!2207564291145.jpg',
      skuPrice:118,
      stock:120,
      num:1,
      skuText:'请选择规格',
  
      // 规格数据结构
      spec:[
        {
          name:'净含量',
          list:[
            {value:'500克',checked:false},
            {value:'1000克',checked:false},
            {value:'1500克',checked:false}
          ]
        }
      ]
    },
  
    /* 收藏 */
    toggleFav(){
      this.setData({isFav:!this.data.isFav});
    },
  
    /* 规格弹窗 */
    openSku(){
      this.setData({showSku:true});
    },
    closeSku(){
      this.setData({showSku:false});
    },
    stopPropagation(){},
  
    /* 选择规格 */
    selectSpec(e) {
const { parent, child } = e.currentTarget.dataset;
const parentIdx = parseInt(parent, 10);
const childIdx = parseInt(child, 10);
const newSpec = this.data.spec.map((group, gIdx) => {
  if (gIdx !== parentIdx) return group;
  return {
    ...group,
    list: group.list.map((item, idx) => ({
      ...item,
      checked: idx === childIdx
    }))
  };
});
this.setData({ spec: newSpec }, () => {
  this.buildSkuText();
});
},
buildSkuText(){
  const arr=this.data.spec.map(g=>{
    const t=g.list.find(i=>i.checked);
    return t?t.value:'';
  }).filter(v=>v);
  this.setData({skuText:arr.length?arr.join('，'):'请选择规格'});
},
checkSku(){
  const ok=this.data.spec.every(g=>g.list.some(i=>i.checked));
  if(!ok){
    wx.showToast({title:'请选择完整规格',icon:'none'});
    return false;
  }
  return true;
},
  
    /* 数量步进器 */
    minus(){
      const n=this.data.num;
      if(n>1) this.setData({num:n-1});
    },
    plus(){
      const n=this.data.num;
      if(n<this.data.stock) this.setData({num:n+1});
    },
  
    /* 加入购物车 / 立即购买 */
    addCart() {
      const cart = wx.getStorageSync('cart') || [];
      const skuId = this.data.skuId || 'default';
      const newItem = {
        id: this.data.id,
        title: this.data.title,
        price: this.data.skuPrice || this.data.price,
        cover: this.data.skuCover || this.data.banner[0],
        num: this.data.num || 1,
        sku: this.data.skuText || '',
        skuId
      };
      // 检查是否已存在
      const idx = cart.findIndex(item => item.id === newItem.id && item.skuId === skuId);
      if (idx > -1) {
        cart[idx].num += newItem.num;
      } else {
        cart.push(newItem);
      }
      wx.setStorageSync('cart', cart);
      wx.showToast({ title: '已加入购物车', icon: 'success' });
    },
    buyNow(){
      if(!this.checkSku()) {
        this.openSkuForBuy();
        return;
      }
      const orderItem = {
        id: this.data.id,
        title: this.data.title,
        price: this.data.skuPrice || this.data.price,
        cover: this.data.skuCover || this.data.banner[0],
        num: this.data.num || 1,
        sku: this.data.skuText || '',
        skuId: this.data.skuId || 'default'
      };
      wx.setStorageSync('currentOrder', [orderItem]);
      wx.navigateTo({url:'/pages/payment/payment'});
    },
    confirmSku(){
      if (!this.checkSku()) return;
      if (this.data.isBuyNow) {
        // 立即购买流程
        this.buyNow();
      } else {
        // 加入购物车流程
        this.addCart();
      }
      this.closeSku();
    },
    openSkuForCart(){
      this.setData({isBuyNow: false, showSku: true});
    },
    openSkuForBuy(){
      this.setData({isBuyNow: true, showSku: true});
    },
  
    /* 工具：校验是否已选规格 */
    checkSku(){
      const ok=this.data.spec.every(g=>g.list.some(i=>i.checked));
      if(!ok){
        wx.showToast({title:'请选择完整规格',icon:'none'});
        return false;
      }
      return true;
    },
  
    /* 分享 */
    onShareAppMessage(){
      return {
        title:this.data.title,
        path:'/pages/goods-detail/goods-detail?id=123'
      }
    }
  })